<template>
  <div class="register">
    <div class="register-weaper">
      <div class="register-right">
        <div class="register-main">
          <h4 class="register-title">
            {{ getThemeConfig.globalTitle }}
          </h4>
          <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form"
                   :label-position="'rigth'" label-width="80px"
          >
            <el-form-item prop="userName" label="账号名">
              <el-input v-model="registerForm.userName" type="text" auto-complete="off" placeholder="请输入账号" />
            </el-form-item>

            <el-form-item prop="password" label="密码">
              <el-input
                v-model="registerForm.password"
                type="password"
                auto-complete="off"
                placeholder="密码"
                @keyup.enter.native="handleRegister"
              />
            </el-form-item>
            <el-form-item prop="confirmPassword" label="确认密码">
              <el-input
                v-model="registerForm.confirmPassword"
                type="password"
                auto-complete="off"
                placeholder="确认密码"
                @keyup.enter.native="handleRegister"
              />
            </el-form-item>
            <el-form-item prop="nickName" label="昵称">
              <el-input v-model="registerForm.nickName" type="text" auto-complete="off" placeholder="请输入昵称" />
            </el-form-item>
            <el-form-item prop="email" label="邮箱">
              <el-input v-model="registerForm.email" type="text" auto-complete="off" placeholder="请输入邮箱" />
            </el-form-item>
            <el-form-item prop="email" label="手机号">
              <el-input v-model="registerForm.phonenumber" type="text" auto-complete="off" placeholder="请输入手机号码" />
            </el-form-item>
            <el-form-item prop="sex" label="性别">
              <dict-select v-model="registerForm.sex" :dict-key="'sys_user_sex'" />
            </el-form-item>
            <el-form-item style="width:100%;">
              <el-button
                :loading="loading"
                size="medium"
                type="primary"
                style="width:100%;"
                @click.native.prevent="handleRegister"
              >
                <span v-if="!loading">注 册</span>
                <span v-else>注 册 中...</span>
              </el-button>
              <div style="float: right;">
                <el-button type="text" @click="$router.push('/login')">
                  使用已有账户登录
                </el-button>
              </div>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <!--  底部  -->
    <div class="el-register-footer">
      <div v-html="getThemeConfig.copyright" />
    </div>
  </div>
</template>

<script>

import DictSelect from "../components/DictSelect"
import {register} from "../api/login"
export default {
  name: 'Register',
  components: {DictSelect},
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"))
      } else {
        callback()
      }
    }
    return {
      cookiePassword: '',
      registerForm: {
        userName: "",
        nickName: "",
        password: "",
        confirmPassword: "",
        email: "",
        phonenumber: "",
        sex: '0'
      },
      registerRules: {
        userName: [
          {required: true, trigger: "blur", message: "请输入您的账号"},
          {min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur'}
        ],
        password: [
          {required: true, trigger: "blur", message: "请输入您的密码"},
          {min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur'}
        ],
        confirmPassword: [
          {required: true, trigger: "blur", message: "请再次输入您的密码"},
          {required: true, validator: equalToPassword, trigger: "blur"}
        ],
        email: [
          {type: 'email', message: '\'请输入正确的邮箱地址', trigger: ['blur', 'change']}
        ],
        phonenumber: [
          {pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: '请输入正确的手机号码', trigger: 'blur'}
        ]
      },
      loading: false
    }
  },
  computed: {
    // 获取布局配置信息
    getThemeConfig() {
      return this.$store.state.themeConfig.themeConfig
    }
  },
  created() {
  },
  methods: {
    handleRegister() {
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          this.loading = true
          register(this.registerForm).then((res) => {
            this.msgSuccess('注册成功')
            this.$router.push("/login")
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.register {
  height: 100%;
  width: 100%;
  overflow: hidden;
  display: flex;
  position: relative;
  background: url('~@/assets/images/bg-login.jpg') no-repeat;
  background-size: cover;

  .el-register-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }

  .register-weaper {
    margin: auto;
    height: 550px;
    display: flex;
    box-sizing: border-box;
    position: relative;
    z-index: 1;
    border: none;
    opacity: 0.8;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);

    .register-right {
      width: 491px;
      padding: 20px;
      position: relative;
      align-items: center;
      display: flex;
      background-color: rgba(255, 255, 255, 1);
      border-radius: 20px;

      .register-main {
        margin: 0 auto;
        width: 70%;
        height: 100%;

        .register-title {
          color: #333;
          margin-bottom: 40px;
          font-size: 22px;
          text-align: center;
          letter-spacing: 4px;
          font-weight: bold;
        }

        .register-form {
          margin: 10px 0;

          .el-form-item {
            margin-bottom: 20px !important;
          }
        }
      }
    }
  }
}

@media screen and (max-width: 500px) {
  .register-left {
    display: none !important;
  }
  .register-right {
    padding: 0px !important;
    width: 365px !important;
    background-color: rgba(255, 255, 255, 0.5) !important;
  }
}
</style>
